package com.shambles.manage2.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.shambles.manage2.service.QuarantineStuffService;
import com.shambles.manage2.tkmapper.entity.MessageRequestVM;
import com.shambles.manage2.tkmapper.entity.QuarantineStuff;
import com.shambles.manage2.tkmapper.entity.RestResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

/**
 * 材料列表接口
 */
@RestController
@RequestMapping("quarantineStuff")
public class QuarantineStuffController {

    @Autowired
    private QuarantineStuffService quarantineStuffService;

    /**
     * 查询材料
     */
    @PostMapping("queryStuff")
    public RestResponse queryStuff(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException {
        MessageRequestVM messageRequestVM=new MessageRequestVM();
        BeanUtils.populate(messageRequestVM,jsonObject);
        PageInfo<QuarantineStuff> pageInfo =  quarantineStuffService.querQuarantineStuffAll(messageRequestVM);
        return RestResponse.ok(pageInfo);
    }

    /**
     * 添加材料
     */
    @PostMapping("insertStuff")
    void insertStuff(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException{
        QuarantineStuff quarantineStuff = new QuarantineStuff();
        BeanUtils.populate(quarantineStuff,jsonObject);
        this.quarantineStuffService.insertQuarantineStuff(quarantineStuff);
    }

    /**
     * 修改材料
     */
    @PostMapping("updatetStuff")
    RestResponse updatetStuff(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException{
        QuarantineStuff quarantineStuff = new QuarantineStuff();
        BeanUtils.populate(quarantineStuff,jsonObject);
        this.quarantineStuffService.updateQuarantineStuff(quarantineStuff);
        return RestResponse.ok();
    }

    /**
     * 删除材料
     */
    @PostMapping("deleteStuff")
    RestResponse deleteStuff(@RequestBody JSONObject jsonObject){
        String guid= jsonObject.getString("guid");
        this.quarantineStuffService.deleteQuarantineStuff(guid);
        return RestResponse.ok();
    }


}
